/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import data.AdminData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Muhammad Dony
 */
public class AdminDB extends ConnectDB {
    private String sql;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    
    public List getdataAdmin() throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `administrator`";
        return getadmindata(sql);
    }
    
    public List getdataAdminById(String ID) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `administrator` WHERE `IDmimin`='"+ID+"';";
        return getadmindata(sql);
    }
        
     private List getadmindata(String sql) throws SQLException, ClassNotFoundException{
        // mempersiapkan list dataPelaporan
        List listdata = new ArrayList();
        //membuat koneksi , createConnection terdapat dalam DBClass
        con = createConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        // mempersiapkan object dataPelaporan
        AdminData listAdmin;
        
        while(rs.next()){
            listAdmin = new AdminData();
            listAdmin.setID(rs.getString(1));
            listAdmin.setUsername(rs.getString(2));
            listAdmin.setFirstname(rs.getString(3));
            listAdmin.setLastname(rs.getString(4));
            listAdmin.setEmail(rs.getString(5));
            listAdmin.setPassword(rs.getString(6)); 
            listAdmin.setLabel(rs.getString(7));
            listAdmin.setID2(rs.getString(8));
            listdata.add(listAdmin);
        }
        // menutup semua koneksi db
        rs.close();
        stmt.close();
        con.close();
        //return value list data pelaporan
        return listdata;
    }
    
    private void actdata(String sql) throws ClassNotFoundException, SQLException{
        // membuat koneksi
        con = createConnection();
        stmt = con.createStatement();
        // execute query
        stmt.execute(sql);
        // menutup koneksi db
        stmt.close();
        con.close();
    }
    public void insertData(AdminData listAdmin) throws ClassNotFoundException, SQLException{
        // query
        sql = "INSERT INTO `scholarship`.`administrator` (`IDmimin`,`usernamemimin`, `firstnameadmin`, `lastnameadmin`, `emailadmin`, `passwordmimin`, `labeladmin`, `ID2`) VALUES ('"+listAdmin.getID()+"', '"+listAdmin.getUsername()+"', '"+listAdmin.getFirstname()+"', '"+listAdmin.getLastname()+"', '"+listAdmin.getEmail()+"', '"+listAdmin.getPassword()+"', '"+listAdmin.getLabel()+"', '"+listAdmin.getID2()+"');";
        // memanggil method untuk execute query
        actdata(sql);

    }
    public void updateData(AdminData listAdmin) throws ClassNotFoundException, SQLException{
        // query
        sql = "UPDATE `administrator` SET `usernamemimin`='"+listAdmin.getUsername()+
                "',`firstnameadmin`='"+listAdmin.getFirstname()+
                "',`lastnameadmin`='"+listAdmin.getLastname()+
                "',`emailadmin`='"+listAdmin.getEmail()+
                "',`passwordmimin`='"+listAdmin.getPassword()+
                "',`labeladmin`='"+listAdmin.getLabel()+
                "',`ID2`='"+listAdmin.getID2()+
                "' WHERE `IDmimin`='"+listAdmin.getID()+"';";
        // memanggil method untuk execute query
        actdata(sql);
    }
    
    public void updateProfil(AdminData listAdmin) throws ClassNotFoundException, SQLException{
        // query
        sql = "UPDATE `administrator` SET `usernamemimin`='"+listAdmin.getUsername()+
                "',`firstnameadmin`='"+listAdmin.getFirstname()+
                "',`lastnameadmin`='"+listAdmin.getLastname()+
                "',`emailadmin`='"+listAdmin.getEmail()+
                "',`passwordmimin`='"+listAdmin.getPassword()+
                "' WHERE `IDmimin`='"+listAdmin.getID()+"';";
        // memanggil method untuk execute query
        actdata(sql);
    }
    
    public void deleteData(String ID) throws ClassNotFoundException, SQLException{
        sql = "DELETE FROM `administrator` WHERE `IDmimin`='"+ID+"';";
        actdata(sql);
    }
}
